package com.hunktimes.wholesale.util

import android.view.Gravity
import android.view.LayoutInflater
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.LayoutRes
import com.hunktimes.wholesale.App
import com.hunktimes.wholesale.R

object ToastUtil {
    private var lastToast: Toast? = null

    private fun showToast(@LayoutRes resource: Int, message: String) {
        if (null != lastToast)
            lastToast!!.cancel()
        val currentToast = Toast(App.get().applicationContext).apply {
            setGravity(Gravity.CENTER, 0, 0)
            duration = Toast.LENGTH_SHORT
            val tstView = LayoutInflater.from(App.get().applicationContext)
                .inflate(resource, null, false)
            val msgTv = tstView.findViewById<TextView>(R.id.toast_tv)
            msgTv.text = message
            view = tstView

        }

        currentToast.show()
        lastToast = currentToast
    }

    fun showSuccessToast(msg: String) {
        showToast(R.layout.toast_success, msg)
    }

    fun showSimpleToast(msg: String) {
        showToast(R.layout.toast_simple, msg)
    }

    fun showErrorToast(msg: String) {
        showToast(R.layout.toast_error, msg)
    }
}